import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.*;

// 学生类
class Student {
    private String name;
    private Map<String, Double> grades; // 科目和对应的成绩

    public Student(String name) {
        this.name = name;
        this.grades = new HashMap<>();
    }

    public void addGrade(String course, double grade) {
        grades.put(course, grade);
    }

    public double calculateGPA() {
        double sum = 0;
        for (double grade : grades.values()) {
            sum += convertToGPA(grade);
        }
        return grades.size() > 0 ? sum / grades.size() : 0.0;
    }

    public String getName() {
        return name;
    }

    public Map<String, Double> getGrades() {
        return grades;
    }

    // 将百分制成绩转换为GPA
    private double convertToGPA(double score) {
        if (score >= 90) return 4.0;
        else if (score >= 80) return 3.0;
        else if (score >= 70) return 2.0;
        else if (score >= 60) return 1.0;
        else return 0.0;
    }
}

// 主类
public class StudentGPASystem {
    private Map<String, Student> students; // 学生姓名和学生对象的映射

    public StudentGPASystem() {
        students = new HashMap<>();
    }

    public void addStudent(String name) {
        students.putIfAbsent(name, new Student(name)); // 使用putIfAbsent以避免重复创建学生对象
    }

    public void addGrade(String studentName, String course, double grade) {
        Student student = students.get(studentName);
        if (student == null) {
            System.out.println("Student not found, adding new student: " + studentName);
            addStudent(studentName); // 如果学生不存在，则添加学生
            student = students.get(studentName); // 重新获取学生对象
        }
        student.addGrade(course, grade);
    }

    public double getGPA(String studentName) {
        Student student = students.get(studentName);
        if (student != null) {
            return student.calculateGPA();
        } else {
            System.out.println("Student not found");
            return 0.0;
        }
    }

    // 从TXT文件导入学生成绩
    public void importFromTxt(String filePath) {
        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = br.readLine()) != null) {
                String[] data = line.split("\\\\"); // 使用反斜杠分隔
                if (data.length < 3) continue; // 跳过不完整的行
                String studentName = data[0];
                String course = data[1];
                double grade = Double.parseDouble(data[2]);
                addGrade(studentName, course, grade);
            }
        } catch (IOException e) {
            System.out.println("Error importing from TXT: " + e.getMessage());
        } catch (NumberFormatException e) {
            System.out.println("Error parsing grade. Please check the file format.");
        }
    }

    // 导出学生成绩到TXT文件
    public void exportToTxt(String filePath) {
        try (BufferedWriter bw = new BufferedWriter(new FileWriter(filePath))) {
            for (Map.Entry<String, Student> entry : students.entrySet()) {
                Student student = entry.getValue();
                bw.write(student.getName());
                for (Map.Entry<String, Double> gradeEntry : student.getGrades().entrySet()) {
                    bw.write("\\" + gradeEntry.getKey() + "\\" + gradeEntry.getValue());
                }
                bw.newLine();
            }
        } catch (IOException e) {
            System.out.println("Error exporting to TXT: " + e.getMessage());
        }
    }

    public static void main(String[] args) {
        StudentGPASystem system = new StudentGPASystem();
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("1. Add Student\n2. Add Grade\n3. Calculate GPA\n4. Import from TXT\n5. Export to TXT\n6. Exit");
            System.out.print("Choose an option: ");
            int option = scanner.nextInt();
            scanner.nextLine(); // consume newline

            switch (option) {
                case 1:
                    System.out.print("Enter student name: ");
                    String name = scanner.nextLine();
                    system.addStudent(name);
                    break;
                case 2:
                    System.out.print("Enter student name: ");
                    name = scanner.nextLine();
                    System.out.print("Enter course name: ");
                    String course = scanner.nextLine();
                    System.out.print("Enter grade: ");
                    double grade = scanner.nextDouble();
                    scanner.nextLine(); // consume newline
                    system.addGrade(name, course, grade);
                    break;
                case 3:
                    System.out.print("Enter student name: ");
                    name = scanner.nextLine();
                    double gpa = system.getGPA(name);
                    System.out.println(name + "'s GPA: " + gpa);
                    break;
                case 4:
                    System.out.print("Enter import file path: ");
                    String importPath = scanner.nextLine();
                    system.importFromTxt(importPath);
                    break;
                case 5:
                    System.out.print("Enter export file path: ");
                    String exportPath = scanner.nextLine();
                    system.exportToTxt(exportPath);
                    break;
                case 6:
                    System.out.println("Exiting...");
                    scanner.close();
                    return;
                default:
                    System.out.println("Invalid option. Please try again.");
            }
        }
    }
}